/*!
 * @file cmd.cpp
 * @brief Ciało cmd.hh
 */

#include "engine/cmd.hh"
using namespace std;

/*--------------------------------------------------------------------------*/

std::stringstream *cmd::activeSStream = NULL;
std::list<actionBase*> *cmd::actionList = new std::list<actionBase*>;
bool cmd::initDone = cmd::start();

/*--------------------------------------------------------------------------*/

cmd::cmd() {
}

/*--------------------------------------------------------------------------*/

bool cmd::start() {
	cmd::addAction(new actionInit());
	cmd::addAction(new actionQuit());
	cmd::addAction(new actionEcho());
	cmd::addAction(new actionSet());
	cmd::addAction(new actionInclude());

	cmd::execute("include init");
	return true;
}

/*--------------------------------------------------------------------------*/

void cmd::executeFile(string path) {

	ifstream file(path.c_str());

	if (!file.is_open()){
		stringstream ss;
		ss << "Nie udało się otworzyć pliku " << path;

		log::error(ss.str());

		return;
	}

	const int lineLength = 512;
	char tmpt[lineLength];


	while (file.getline(tmpt, lineLength)) {

		if (tmpt[0] == '#')
			continue;

		if (tmpt[0] == '\n')
			continue;

		stringstream ss;
		ss << tmpt;

		cmd::execute(ss.str());
	}

	file.close();
}

/*--------------------------------------------------------------------------*/

void cmd::execute(string l) {

	int count = 0;

	/*
	 * Zliczanie ilości wyrazów
	 */
	stringstream stream(l);
	string str;

	while (stream >> str) {
		++count;
	}

	string array[count];

	/*
	 * Uzupełnianie tablicy danymi
	 */
	stringstream ss(l);
	int i = 0;
	while (ss >> array[i]) {
		++i;
	}

	cmd::callAction(array, count);
}

/*--------------------------------------------------------------------------*/

void cmd::setSStream(std::stringstream *ss) {
	cmd::activeSStream = ss;
}

/*--------------------------------------------------------------------------*/

void cmd::addAction(actionBase *action) {
	cmd::actionList->push_back(action);
}

/*--------------------------------------------------------------------------*/

void cmd::callAction(std::string *array, int count) {

	if (count < 0) {
		return;
	}

	for (list<actionBase*>::iterator i = cmd::actionList->begin(); i
			!= cmd::actionList->end(); ++i) {
		if ((*i)->getAlias() == array[0]) {

			if (cmd::activeSStream == NULL) {
				stringstream devnull;
				(*i)->doAction(array, count, &devnull);
			} else {
				(*i)->doAction(array, count, cmd::activeSStream);
			}

			return;
		}
	}

	cout << "Nie znalezniono akcji dla: " << array[0] << endl;
}

/*--------------------------------------------------------------------------*/

/*


 string cmd::cmd_funcSelection(string *array, int count) {

 try {

 if (array[0] == "echo") { //Wypisywanie komunikatu
 return pawirr::cmd_cEcho(array, count);

 } else if (array[0] == "quit") { //Kończnie programu
 return pawirr::cmd_cQuit(array, count);

 } else if (array[0] == "list") { //Listowanie zmiennych
 return pawirr::cmd_cList(array, count);

 } else if (array[0] == "sudo") { //Sterowanie trybami
 return pawirr::cmd_cSudo(array, count);

 } else if (pawirr::set->exists(array[0])) { //Szukanie na liście zmiennych
 string sec = array[1];

 if (sec.length() <= 0) {//pobranie

 stringstream ss2;
 ss2 << array[0] << " = " << pawirr::set->get(array[0])->s
 << endl;
 return ss2.str();

 } else {//nadanie

 pawirr::set->put(array[0], sec);
 return "ok\n";

 }
 }
 //Nieznana komenda
 else {
 throw LANG_CMD_UNKNOWN_CMD;
 }

 } catch (const char *msg) {
 stringstream ss;
 ss << "#1" << LANG_CMD_ERR_HEADER << ":" << endl << "#1" << msg << endl;

 return ss.str();
 }
 }

 /*--------------------------------------------------------------------------*

 string pawirr::cmd_cList(string *array, int count) {

 return pawirr::set->listAll();

 }

 /*--------------------------------------------------------------------------*

 string pawirr::cmd_cSudo(string *array, int count) {

 /*
 * 0 = sudo
 * 1 = nazwa trybu
 * 2 = akcja do wykonania na trybie
 *

 if (count < 3)
 throw LANG_CMD_TOO_FEW_ARG;

 modeBase *active = pawirr::sys_getModeByName(array[1]);

 if (active != NULL) {
 if (array[2] == "ma_off") {
 active->setActive = MA_OFF;

 } else if (array[2] == "ma_on") {
 active->setActive = MA_ON;

 } else if (array[2] == "ma_always") {
 active->setActive = MA_ALWAYS;
 }

 } else {
 throw LANG_CMD_NOT_FOUND;

 }

 return string("ok\n");
 }

 /*--------------------------------------------------------------------------*/
